package com.qf.flash.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.PhotoLinesBean;
import com.qf.flash.utils.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class HeadFrameAdapter extends RecyclerView.Adapter<HeadFrameAdapter.MyViewHolder> {
    private Activity context;

    public HeadFrameAdapter(Activity context, List<PhotoLinesBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    private List<PhotoLinesBean> datas;

    @NonNull
    @Override
    public HeadFrameAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_head_frame, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        PhotoLinesBean photoLinesBean = datas.get(position);
        ImageLoader.loadFit(context, photoLinesBean.getImage(), holder.grid_img);
//        Glide.with(context)
//                .load(photoLinesBean.getImage()).into(holder.grid_img);
        // 是否选中
        if (photoLinesBean.getIsSelected() == 0) {
            holder.img_choose.setVisibility(View.GONE);
        } else {
            holder.img_choose.setVisibility(View.VISIBLE);
        }

        // 解锁条件
        if (photoLinesBean.getMvpCount() > 0) {
            holder.down_txt.setText("获得" + photoLinesBean.getMvpCount() + "次Mvp后解锁");
        } else if (photoLinesBean.getYqCount() > 0) {
            holder.down_txt.setText("获得" + photoLinesBean.getYqCount() + "次勇气后解锁");
        } else if (photoLinesBean.getZhCount() > 0) {
            holder.down_txt.setText("获得" + photoLinesBean.getZhCount() + "次智力担当后解锁");
        } else if (photoLinesBean.getYzCount() > 0) {
            holder.down_txt.setText("获得" + photoLinesBean.getYzCount() + "次颜值担当后解锁");
        }

        // 是否解锁
        if (photoLinesBean.getIsOpen() == 0) {
            holder.ll_lock.setVisibility(View.VISIBLE);
        } else {
            holder.ll_lock.setVisibility(View.GONE);
        }
        holder.rlContent.setOnClickListener(v -> {
            if (photoLinesBean.getIsOpen() != 0) {
                EventBus.getDefault().post(photoLinesBean);
                context.finish();
            }

        });
    }

    @Override
    public int getItemCount() {

        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView grid_img;
        private ImageView img_choose;
        private TextView up_txt;
        private TextView down_txt;
        private LinearLayout ll_lock;
        private ConstraintLayout rlContent;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            grid_img = itemView.findViewById(R.id.iv_head_circle);
            img_choose = itemView.findViewById(R.id.iv_choose);
            up_txt = itemView.findViewById(R.id.tv_up);
            down_txt = itemView.findViewById(R.id.tv_down);
            ll_lock = itemView.findViewById(R.id.ll_locked);
            rlContent = itemView.findViewById(R.id.rl_content);
        }
    }

    public void setDatas(List<PhotoLinesBean> photoLinesBeans) {
        if (null != photoLinesBeans) {
            this.datas.clear();
            this.datas.addAll(photoLinesBeans);
            notifyDataSetChanged();
        }
    }

}
